MapBrowserPortlet = Class.extend({
	init: function()	{
		this.name = "MapBrowserPortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
		this.map = new Map();
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	onMapSearchKeyup: function(event)	{
		var keycode = event.keyCode;
		if (keycode != 13) return;
		var txt = this.requestForEffectiveResource('SearchInput').val();
		this.map.searchAddress(txt);
	},
	
	run: function()	{
		var obj = this;
		this.getPortletPlaceholder().paintCanvas(this.render());
		this.map.setMap("MapBrowserPortlet-Map");
		var loggedin = SingletonFactory.getInstance(Application).getSystemProperties().get('user.login');
		if (loggedin == 1)	{
			this.requestForEffectiveResource('Control').html(this.renderView('LoggedIn', {}));
			this.map.locate(function(position, txt) {
				obj.markLocation(position, txt);
			});
		} else {
			this.requestForEffectiveResource('Control').html(this.renderView('Default', {}));
		}
	},
	
	markLocation: function(position, txt){
		this.map.markLocation(position, txt);
		this.map.deleteAllMarker();
		this.getNearPlaces();
		this.getNearFriends();
		this.getNearUsers();
		this.saveLocation();
	},
	
	saveLocation: function(){
		var obj = this;
		this.onAjax('user', 'save-location', {lat:this.map.mklat,lng:this.map.mklng}, 'GET', {
			'onSuccess': function(ret)	{
			}
		});
	},
	
	getNearPlaces: function(){
		var obj = this;
		this.onAjax('user', 'get-near-places', {lat:this.map.mklat,lng:this.map.mklng}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = ret;
				obj.map.showPlaceOnMap(obj.model);
			}
		});
	},
	
	getNearFriends: function(){
		var obj = this;
		this.onAjax('user', 'get-near-friends', {lat:this.map.mklat,lng:this.map.mklng}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = ret;
				obj.map.showFriendOnMap(obj.model);
			}
		});
	},
	
	getNearUsers: function(){
		var obj = this;
		this.onAjax('user', 'get-near-users', {lat:this.map.mklat,lng:this.map.mklng}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = ret;
				obj.map.showUserOnMap(obj.model);
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);